<template>
  <div class="home">
   <el-container>
     <el-aside width="auto">
       <!-- router表示开启路由模式，它会以index作为路劲进行跳转 -->
        <!-- collapse通过布尔值控制水平展开与折叠 -->
        <el-menu
          :collapse="iscollapse"
          class="el-menu-admin"
          router
          :default-active="$route.name"
          @open="handleOpen"
          @close="handleClose">
          <div class="logo"></div>
          <el-submenu :index="item.path" v-for="item in menusList" :key="item.id">
            <template slot="title" >
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
          <el-menu-item :index="submenu.path" v-for="submenu in item.children" :key="submenu.id">
            <i class="el-icon-setting"></i>
            <span slot="title">{{submenu.authName}}</span>
          </el-menu-item>
          </el-submenu>

        </el-menu>
     </el-aside>
     <el-container>
       <el-header>
         <div class="toggle-btn myicon myicon-menu" @click="toggle"></div>
         <div class="system-title">电商后台管理系统</div>
         <div>
           <span class="welcome">您好：{{uname}}</span>
           <el-button type="success" round @click="logout">退出</el-button>
         </div>
       </el-header>
       <el-main>
         <!-- 首先将router-view用transition组件包裹起来 -->
         <transition mode="out-in" enter-active-class="animate bounceIn" leave-active-class="animate bounceOut">
          <router-view></router-view>
         </transition>
       </el-main>
     </el-container>
   </el-container>
  </div>
</template>

<script>
import { getMenus } from '@/api'
export default {
  data () {
    return {
      iscollapse: false, // 控制菜单展开与折叠默认false未展开
      uname: '',
      menusList: []
    }
  },
  mounted () {
    let username = localStorage.getItem('username')
    this.uname = username
    this.initMenus()
  },
  methods: {
    toggle () {
      this.iscollapse = !this.iscollapse
    },
    // 退出登录
    logout () {
      // 跳转登录页面，清楚本地储存
      localStorage.clear()
      this.$router.push({ name: 'login' })
    },
    // 初始化菜单
    initMenus () {
      getMenus()
        .then(res => {
          this.menusList = res.data.data
        })
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    }
  }
}
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo1.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
